package com.eastfair.imaster.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.imaster.dto.CheckEmployeeDTO;
import com.eastfair.imaster.dto.CheckEmployeePageQuery;
import com.eastfair.imaster.dto.CheckEmployeeSaveDTO;
import com.eastfair.imaster.dto.CheckEmployeeUpdateDTO;
import com.eastfair.imaster.entity.CheckEmployee;
import com.eastfair.imaster.entity.Company;
import com.eastfair.imaster.enumeration.CheckEmployeeStatusEnum;
import com.eastfair.imaster.service.CheckEmployeeService;
import com.eastfair.imaster.service.CompanyService;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 公司审核用户表
 * </p>
 *
 * @author ligang
 * @date 2021-08-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/checkEmployee")
@Api(value = "CheckEmployee", tags = "公司审核用户表")
public class CheckEmployeeController extends SuperController<CheckEmployeeService, Long, CheckEmployee, CheckEmployeePageQuery, CheckEmployeeSaveDTO, CheckEmployeeUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Autowired
    private CompanyService companyService;

    @Override
    public void handlerResult(IPage<CheckEmployee> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<CheckEmployee> checkEmployeeList = list.stream().map((map) -> {
            CheckEmployee checkEmployee = CheckEmployee.builder().build();
            //TODO 请在这里完成转换
            return checkEmployee;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(checkEmployeeList));
    }


    /**
     * 将用户拉入到公司，进行审核
     *
     * @param checkEmployeeDTO companyId     公司id
     *                         userAccountId 用户账户id
     *                         isCheck       是否需要验证
     *                         isAdamin      是否需要成为管理员
     */
    @ApiOperation(value = "用户加入到公司，等待审核", notes = "将用户拉入到公司，进行审核")
    @PostMapping("/addUserIdOfCompany")
    public R addUserIdOfCompany(@RequestBody @Validated({CheckEmployeeDTO.addUserIdOfCompany.class}) CheckEmployeeDTO checkEmployeeDTO) {
        //公司是否已认证
        List<Company> companyList = companyService.list(null, Arrays.asList(checkEmployeeDTO.getCompanyId()));
        if(companyList==null || companyList.isEmpty()){
            //公司不存在
            return R.fail(ExceptionCode.COMPANY_NULL);
        }
        if(companyList.get(0).getIsAuthentication()== BusinessConstant.NO){
            //公司未认证
            return R.fail(ExceptionCode.COMPANY_NO_AUTH);
        }
        //校验用户是否可以加入公司
        R r = baseService.verifyCompanyIsCheck(checkEmployeeDTO.getUserAccountId());
        if(r.getCode() != ExceptionCode.SUCCESS.getCode()){
            return r;
        }
        return baseService.addUserIdOfCompany(checkEmployeeDTO.getCompanyId(), checkEmployeeDTO.getUserAccountId(), true, checkEmployeeDTO.getIsAdmin());
    }

    /**
     * 审核用户成为员工
     *
     * @param checkEmployeeDTO
     */
    @ApiOperation(value = "审核用户成为员工", notes = "审核用户成为员工")
    @PostMapping("/checkUserIdOfCompany")
    public R checkUserIdOfCompany(@RequestBody @Validated({CheckEmployeeDTO.checkUserIdOfCompany.class}) CheckEmployeeDTO checkEmployeeDTO) {
        return baseService.checkUserIdOfCompany(checkEmployeeDTO.getId(), checkEmployeeDTO.getIsConsent()==1?true:false);
    }


    /**
     * 取消审核用户成为公司员工
     *
     * @param checkEmployeeDTO
     */
    @ApiOperation(value = "取消审核用户成为公司员工", notes = "取消审核用户成为公司员工")
    @PostMapping("/cancelUserIdOfCompany")
    public R cancelUserIdOfCompany(@RequestBody @Validated({CheckEmployeeDTO.cancelUserIdOfCompany.class}) CheckEmployeeDTO checkEmployeeDTO) {
        return baseService.cancelUserIdOfCompany(checkEmployeeDTO.getId());
    }


    /**
     * 查询公司下审核的员工
     *
     * @param companyId 公司id
     * @param status    审核状态
     * @return
     */
    @ApiOperation(value = "查询公司下审核的员工", notes = "查询公司下审核的员工")
    @GetMapping("/queryCheckEmployee")
    public R<List<CheckEmployee>> queryCheckEmployee(Long companyId, CheckEmployeeStatusEnum status) {
        return R.success(baseService.queryCheckEmployeeOfCompany(companyId, status));
    }


}
